<!-- 秒杀商品编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="skillCommodityModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="skillCommodityModalApp.skillCommodity"
        :rules="skillCommodityModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
        <a-row :gutter="16">

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="商品:" name="commodityId">
              <m-entity-select
                :default-value="skillCommodityModalApp.skillCommodity.commodityName"
                v-model:value="skillCommodityModalApp.skillCommodity.commodityId"
                placeholder="请选择商品"
                module="good"
                show-options-name="description"
                entity="Commodity"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="规格:" name="specificationsId">
              <m-entity-select
                :default-value="skillCommodityModalApp.skillCommodity.specificationsName"
                v-model:value="skillCommodityModalApp.skillCommodity.specificationsId"
                placeholder="请选择规格"
                module="good"
                show-options-name="specification"
                :query-filters="{'commodityId':skillCommodityModalApp.skillCommodity.commodityId}"
                entity="SpecificationStock"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="库存:" name="stock">
              <a-input-number
                v-model:value="skillCommodityModalApp.skillCommodity.stock"
                placeholder="请输入库存"
                :min="0"
                :step="1"
                class="ele-fluid"
              ></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="价格:" name="price">
              <a-input-number
                v-model:value="skillCommodityModalApp.skillCommodity.price"
                placeholder="请输入价格"
                :min="0"
                :step="0.01"
                class="ele-fluid importanceI"></a-input-number>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="开始时间:" name="startDate">
              <a-date-picker
                v-model:value="skillCommodityModalApp.skillCommodity.startDate"
                placeholder="请选择开始时间"
                showTime
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="结束时间:" name="endDate">
              <a-date-picker
                v-model:value="skillCommodityModalApp.skillCommodity.endDate"
                placeholder="请选择结束时间"
                showTime
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="skillCommodityModalApp.display">
                <teleport to="#skillCommodityEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="skillCommodityListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(skillCommodityModalApp.skillCommodity)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!skillCommodityModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(skillCommodityModalApp.skillCommodity)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {SkillCommodityService} from "@/views/mall/skill/skillCommodity/skillCommodityService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let skillCommodityListApp = inject('skillCommodityListApp', reactive({}));
    let skillCommodityGrid = inject('skillCommodityGrid', reactive({}))
    const skillCommodityModalApp = reactive({
      id: null,
      skillCommodity: {},
      isEdit: false,
      display: false
    });
    skillCommodityModalApp.rules = {
      commodityId: [
        {
          required: true,
          type: 'number',
          message: '请输入商品',
          trigger: 'blur'
        }
      ],
      commodityName: [
        {
          required: false,
          type: 'string',
          message: '请输入商品',
          trigger: 'blur'
        }
      ],
      specificationsName: [
        {
          required: false,
          type: 'string',
          message: '请输入规格',
          trigger: 'blur'
        }
      ],
      specificationsId: [
        {
          required: true,
          type: 'number',
          message: '请输入规格',
          trigger: 'blur'
        }
      ],
      stock: [
        {
          required: true,
          type: 'number',
          message: '请输入库存',
          trigger: 'blur'
        }
      ],
      price: [
        {
          required: true,
          type: 'number',
          message: '请输入价格',
          trigger: 'blur'
        }
      ],
      startDate: [
        {
          required: true,
          type: 'string',
          message: '请输入开始时间',
          trigger: 'blur'
        }
      ],
      endDate: [
        {
          required: true,
          type: 'string',
          message: '请输入结束时间',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      skillCommodityModalApp.skillCommodity = reactive({});
      if (routeId) {//路由
        skillCommodityModalApp.id = routeId;
        skillCommodityModalApp.display = false;
        findSkillCommodity(skillCommodityModalApp.id);
      } else if (skillCommodityListApp.editModalForEdit) {//修改弹窗
        skillCommodityModalApp.id = skillCommodityListApp.currentId;
        skillCommodityModalApp.display = true;
      } else if (!skillCommodityListApp.editModalForEdit && skillCommodityListApp.addModalForEdit) {//新增弹窗
        skillCommodityModalApp.display = true;
      } else {
        skillCommodityModalApp.title = "新增学生";
      }
      if (skillCommodityModalApp.id) findSkillCommodity(skillCommodityModalApp.id);
    })

    const findSkillCommodity = (id) => {
      SkillCommodityService.findSkillCommodityForView(id).then((res) => {
        skillCommodityModalApp.skillCommodity = res.data;
        //TODO:为编辑准备.
        skillCommodityModalApp.title = "编辑学生 " + skillCommodityModalApp.skillCommodity.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找学生出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        SkillCommodityService.updateSkillCommodity(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        SkillCommodityService.saveSkillCommodity(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/mall/skill/skillCommodity`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        skillCommodityGrid.value.commitProxy("query");
      }, 1500);
      skillCommodityListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      skillCommodityListApp,
      skillCommodityModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
